package base.jfinal;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import base.jfinal.anatation.RouteBind;
import base.jfinal.plugin.ClassSearcher;
import base.util.PropertiesContent;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.config.Routes;

/**
 * Routes 工具类 自动绑定Controller
 */
public class RoutesUtil {
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void add(Routes me) {
        List<Class> list = ClassSearcher.findClass("/mt/ctrl", "/pay/ctrl","/weixin/ctrl");
        if (list != null && list.isEmpty() == false) {
            for (Class clz : list) {
                RouteBind rb = (RouteBind) clz.getAnnotation(RouteBind.class);
                if (rb != null) {
                    String clzDir = (new File(clz.getResource("").getPath())).getName();
                    if ("ctrl".equals(clzDir)) {
                        clzDir = "";
                    } else {
                        clzDir += "/";
                    }
                    String route = PropertiesContent.get("jfinal.basepath", "");
                    route += rb.path().startsWith("/") ? rb.path() : (clzDir + rb.path());
                    System.out.println(
                            "Add Route:" + route + " Class:" + clz.getName() + " ViewPath:" + rb.viewPath());
                    me.add(route, clz, rb.viewPath());
                }
            }
        }
    }
}
